/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.recharge.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.framework.base.utils.Constants;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.recharge.entity.Recharge;
import com.boxin.ims.modules.recharge.service.RechargeService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 会员充值Controller
 * @author Jakemanse
 * @version 2014-03-03
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/recharge/recharge")
public class RechargeController extends BaseController {

	@Autowired
	private RechargeService rechargeService;
	
	
	@Autowired
	private QRCodeService qrCodeService;
	
	@ModelAttribute
	public Recharge get(@RequestParam(required=false) Long id) {
		if (id != null){
			return rechargeService.get(id);
		}else{
			return new Recharge();
		}
	}
	
	@RequiresPermissions("recharge:recharge:view")
	@RequestMapping(value = {"list", ""})
	public String list(Recharge recharge, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			recharge.setUser(user);
		}
        Page<Recharge> page = rechargeService.find(new Page<Recharge>(request, response), recharge); 
        model.addAttribute("page", page);
		return "modules/recharge/rechargeList";
	}

	@RequiresPermissions("recharge:recharge:view")
	@RequestMapping(value = "form")
	public String form(Recharge recharge, Model model) {
	
		User user = UserUtils.getUser();
		if (recharge.getUser() == null || recharge.getUser().getId() <1){
			recharge.setUser(user);
		}
		
		model.addAttribute("recharge", recharge);
		return "modules/recharge/rechargeForm";
	}

	@RequiresPermissions("recharge:recharge:edit")
	@RequestMapping(value = "save")
	public String save(Recharge recharge,HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, recharge)){
			return form(recharge, model);
		}
		rechargeService.save(recharge);
		
		
		if(recharge.getQrCode() == null  || recharge.getQrCode().getId()<=0){	//生成二维码
			QRCode qrcode = new QRCode();
			String content = recharge.getMember().getNumber();
			qrcode.setContent(content);
			String path = Constants.getDefaultQRCodeResourcePath();
			path = WechatUtils.getMemberCardResourceSavePath(request);
			FileUtils.createDirectory(path);
			String filePath = path + File.separator+ content;
			
			QRCodeUtils.generateNoMarginQRCodePNG(content, filePath, 300,300);
			qrcode.setFileName(content+".png");
			qrcode.setPngPath(filePath+".png");
			qrCodeService.save(qrcode);
			
			recharge.setQrCode(qrcode);
		}
		
		
		addMessage(redirectAttributes, "保存会员充值'" + recharge.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/recharge/recharge/?repage";
	}
	
	@RequiresPermissions("recharge:recharge:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		rechargeService.delete(id);
		addMessage(redirectAttributes, "删除会员充值成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/recharge/recharge/?repage";
	}

}
